package weixinkeji.dev.dc.buy.order.h.manager;

import sys.wx.ann.hm.M;
import sys.wx.c.ATR;
import sys.wx.c.components.check.bean.CheckData;
import sys.wx.c.components.cv.CV;
import sys.wx.m.db.SQL.mysql.common.where.Where;
import sys.wx.m.db.SQL.mysql.common.where.WhereConfig;
import sys.wx.m.db.dao.mysql.Dao;
import weixinkeji.dev.dc.buy.order.bean.FWYOrder;
import weixinkeji.dev.dc.buy.order.bean.FWYOrderB;
import weixinkeji.dev.dc.buy.order.h.manager.vo.ManagerFWYUpdateBackVo;
import weixinkeji.dev.dc.buy.order.h.manager.vo.ManagerFWYUpdateNumLockVo;
import weixinkeji.dev.dc.buy.order.h.manager.vo.check.ManagerFWYUpdateNumLockCheck;
import weixinkeji.dev.dc.buy.order.so.FWYOrderSO;
import weixinkeji.dev.dc.buy.order.xy.FWYOrderXY;

@sys.wx.ann.hm.H("view/buy/order/manager/fwy/u")

public class ManagerFWYOrderU {

    //查询未结账的单据。
    @M("/uLockFood")//通过 view/buy/order/manager/fwy/u/uLockFood.imy 访问
    public static String uLockFood(ATR a) {
        ManagerFWYUpdateNumLockVo vo = CV.getObjectByFields(ManagerFWYUpdateNumLockVo.class, a.request, ManagerFWYUpdateNumLockCheck.doNumLock());
        if (null == vo) {
            return null;
        }
        FWYOrderB s = Dao.selectOneByID(FWYOrderB.class, vo.bid);
        if (FWYOrderSO.checkOrderIsLock(s.getOrder_id())) {
            a.out_OK_NO(false, "订单已经付款，无法执行上菜操作");
            return null;
        }
        Double lockNum = s.getOrderb_lock_number() + vo.lockNum;
        //如果上台数量大于下单量，或上台数量小于0
        if (lockNum > s.getBuy_number() || lockNum < 0) {
            a.out_OK_NO(false, "无法继续上菜");
            return null;
        } else {
            s.setOrderb_lock_number(lockNum);
            boolean x = FWYOrderSO.updateLockNumAndStyle(s);//Dao.updateAlloy(s, "orderb_lock_number");
            a.out_OK_NO(x, x ? "成功上菜数量：" + vo.lockNum + " " : "上菜时出错！！");
            return null;
        }
    }

    @M("/backFood")//通过 view/buy/order/manager/fwy/u/backFood.imy 访问
    public static String ubackFood(ATR a) {
        ManagerFWYUpdateBackVo vo = CV.getObjectByFields(ManagerFWYUpdateBackVo.class, a.request, ManagerFWYUpdateNumLockCheck.doNumBack());
        if (null == vo) {
            return null;
        }
        FWYOrderB s = Dao.selectOneByID(FWYOrderB.class, vo.bid);
        if (FWYOrderSO.checkOrderIsLock(s.getOrder_id())) {
            a.out_OK_NO(false, "订单已经付款，无法执行退菜操作");
            return null;
        } else if (FWYOrderSO.getSum_BuyNumber(vo.order_id, vo.food_id) + vo.backNum < 0) {
            a.out_OK_NO(false, "对不起，客户并未下购这么多数量。不足数量退！");
            return null;
        }
        Where w = new Where(new WhereConfig(""));
        w.put(Where.TJ_IGNORE_NOTHING, "", "order_id", "=", vo.order_id)
                .put(Where.TJ_IGNORE_NOTHING, Where.AND, "source_id", "=", vo.food_id)
                .put(Where.TJ_IGNORE_NOTHING, Where.AND, "buy_sum", "<", 0);

        FWYOrderB fb = Dao.selectOneByTJ(FWYOrderB.class, w.getWhere());
        fb.setOrderb_lock_number(0.0);
        fb.setOrderb_style(FWYOrderXY.B_STYLE_2);

        if (null != fb.getOrderb_id()) {
            fb.setBuy_number((fb.getBuy_number() * 1000 + vo.backNum * 1000) / 1000);
            fb.setBuy_sum(fb.getBuy_number() * 1000 * fb.getBuy_price() / 1000);
            a.out_OK_NO(FWYOrderSO.updateOrderB2(fb));
            return null;
        }
        s.setBuy_number(vo.backNum);
        s.setOrderb_lock_number(0.0);
        s.setOrderb_style(FWYOrderXY.B_STYLE_2);
        s.setBuy_sum(vo.backNum * 1000 * s.getBuy_price() / 1000);
        a.out_OK_NO(FWYOrderSO.addOrderB(s));
        return null;
    }

    @M("/songruChufang")//通过 view/buy/order/manager/fwy/u/songruChufang.imy 访问
    public static String docreateFood(ATR a) {
        String order_id = CV.getParameter(a.request, "order_id", CheckData.ID);
        if (null == order_id) {
            return null;
        }
        a.out_OK_NO(FWYOrderSO.songruChufang(order_id));
        return null;
    }

    @M("/println")//通过 view/buy/order/manager/fwy/u/println.imy 访问
    public static String printlnOrder(ATR a) {
        String order_id = CV.getParameter(a.request, "order_id", CheckData.ID);
        if (null == order_id) {
            return null;
        }

        FWYOrder fobj = Dao.selectOneByID(FWYOrder.class, order_id);
        fobj.setOrder_print(null == fobj.getOrder_print() ? 1 : fobj.getOrder_print() + 1);
        Dao.updateAlloy(fobj, "order_print");
        return null;
    }

}
